import math

# greatest common divisor
def gcd(a, b):
    return a if b==0 else gcd(b, a%b)

# lowest common multiple
def lcm(a, b):
    return a * b / gcd(a, b)


def find_proper_divisors(n):
    divisors = []
    for i in range(2, int(math.sqrt(n)+1)): 
        if n % i == 0:
            divisors += [i] 
            if n != i * i:
                divisors += [n / i] 
    return divisors + [1] if n != 1 else []

def find_divisors(n):
    return  find_proper_divisors(n) + [n]


# generate pandigitals
def pandigitals(digitals, length):
    if length == 1:
        return map(str, digitals)
    else:
        ret = []
        for x in digitals:
            new_d = digitals[:]
            new_d.remove(x)
            ret += map(lambda s : str(x)  + s, pandigitals(new_d, length-1))
        return ret


# Sieve of Eratosthenes
def xprimes(limit):
    # Initialize the primality list
    a = [True] * limit  
    a[0] = a[1] = False
    
    for (i, isprime) in enumerate(a):
        if isprime:
            yield i
            # Mark factors non-prime
            for n in xrange(i*i, limit, i):  
                a[n] = False
